<template>
  <el-aside :width="$store.state.isCollapse?'64px':'180px'">
    <el-menu
      class="el-menu-vertical-demo"
      :collapse="$store.state.isCollapse"
      :collapse-transition="false"
      background-color="#545c64"
      text-color="white"
    >
      <h3 v-show="!$store.state.isCollapse">后台管理</h3>
      <h3 v-show="$store.state.isCollapse">后台</h3>

      <el-menu-item v-for="item in noChildren" :key="item.path" :index="item.path" @click="clickMenu(item)">
        <component class="icons" :is="item.icon"></component>
        <span>{{item.label}}</span>
      </el-menu-item>

      <el-sub-menu v-for="item in hasChildren" :key="item.path" :index="item.label">
        <template #title>
          <el-icon>
            <component class="icons" :is="item.icon"></component>
          </el-icon>
          <span>{{item.label}}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
            :index="subItem.path"
            v-for="(subItem,subIndex) in item.children"
            :key="subIndex"
            @click="clickMenu(subItem)"
          >
            <el-icon>
              <component class="icons" :is="subItem.icon"></component>
            </el-icon>
            <span>{{subItem.label}}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script>
import {useRouter} from 'vue-router'
import {useStore} from 'vuex'
export default {
  setup() {
    const router=useRouter()
    const store=useStore()
    const list = [
      {
        path: "/user",
        name: "user",
        label: "用户管理",
        icon: "user",
        url: "UserManage/UserManage"
      },
      {
        label: "其他",
        icon: "location",
        path: "/other",
        children: [
          {
            path: "/page1",
            name: "page1",
            label: "页面1",
            icon: "setting",
            url: "Other/PageOne"
          },
          {
            path: "/page2",
            name: "page2",
            label: "页面2",
            icon: "setting",
            url: "Other/PageTwo"
          }
        ]
      }
    ];

    // 数据分类。按照list里面的数据是否有children字段进行分类
    const noChildren = list.filter(item => !item.children);
    const hasChildren = list.filter(item => item.children);

    // 点击菜单进行路由的跳转
    const clickMenu=(item)=>{
      router.push({
        name:item.name
      })

      // vuex
      store.commit('selectMenu',item)
    }

    return { noChildren, hasChildren, clickMenu };
  }
};
</script>

<style lang="less" scoped>
.icons {
  height: 16px;
  width: 16px;
  margin-right: 5px;
}

.el-menu {
  border-right: none;
  & > h3 {
    text-align: center;
    line-height: 50px;
    color:white;
  }
}
</style>